package com.cn.wanxi.util;

import com.cn.wanxi.model.ResultModel;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebFilter("/*")
public class MyFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //        1.乱码解决
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("UTF-8");

        response.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
        response.setHeader("Access-Control-Allow-Credentials", "true");
//        在放行之前，需要判断是否有用户登陆，如果有，则放行，没有，则提示请登陆
//        需要拿到用户名来判断是否登陆，需要用session存放用户名
//        四大作用域  page,request session application
        String path = request.getServletPath();
//        System.out.println(path);

//        String paths[] = {"/UserFindByUserIdServlet","/UserLoginServlet","/UserRegiterServlet","/back/userLogin.jsp", "/code"};
        List<String> list = new ArrayList<>();
        list.add("/UserFindByUserIdServlet");
        list.add("/UserLoginServlet");
        list.add("/UserRegiterServlet");
        list.add("/back/userLogin.jsp");
        list.add("/code");
        list.add("/home");
        list.add("/company");
        list.add("/news");
        list.add("/product");
        list.add("/online");
        list.add("/callus");
        list.add("/common");

        boolean fangxing = false;
        for (String pa : list) {
            if (pa.equals(path)) {
                fangxing = true;
                break;
            }
        }
        String files[] = {"/jsp/", "/js/", "/css/", "/img/"};
        for (String aa : files) {
            if (path.startsWith(aa)) {
                fangxing = true;
                break;
            }
        }

        if (fangxing) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            Jedis jedis = new Jedis();
            String temp = request.getParameter("token");
            String username = null;
            if (temp != null) {
                username = jedis.get(temp);
            }
//            String username = (String) request.getSession().getAttribute("username");
            if (username == null) {
                ResultModel resultModel = new ResultModel();
                resultModel.setCode(400);
                JSONArray jsonArray = JSONArray.fromObject(resultModel);
                response.getWriter().println(jsonArray);
//                response.sendRedirect("/back/userLogin.jsp");
            } else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }
}
